package com.tozed.android.launcher.simpleui;

import com.tozed.android.launcher.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SimpleFolderIcon extends RelativeLayout
{
	private static final String DEFAULT_BG = "bg_folder";
	private TextView mAppTitle;
	private Context mContext;
	private boolean mIsSystemAppFolder;
	private Drawable mOnTouchDrawble;
	private SimpleFolderPreView mSimpleFolderPreView;
	
	Runnable mDrawOutlineRunnable = new Runnable()
	{
		public void run()
		{
			if (!isPressed())
				setPressed(true);
			postInvalidate();
		}
	};
	
	Handler mPostDrawHandler = new Handler(){
		
	};
	  
	public SimpleFolderIcon(Context context){
		this(context, null);
	}

	public SimpleFolderIcon(Context context, AttributeSet attrs){
		this(context, attrs, 0);
	}

	public SimpleFolderIcon(Context context, AttributeSet attrs, int defStyle){
		super(context, attrs, defStyle);
		mContext = context;
	}
	
	// 获得相关控件
	@Override
	protected void onFinishInflate()
	{
		super.onFinishInflate();
		
		Resources res = mContext.getResources();
	    mAppTitle = ((TextView)findViewById(R.id.simple_folder_title));
	    mOnTouchDrawble = res.getDrawable(R.drawable.bg_pressed);
	    mSimpleFolderPreView = ((SimpleFolderPreView)findViewById(R.id.simple_folder_preview));
	}

	// 设置文件夹内容
	public void setContentApps() {
		if (mIsSystemAppFolder){
			mSimpleFolderPreView.setContent(SimpleLauncherModel.sOtherApps);
		}else{
			mSimpleFolderPreView.setContent(SimpleLauncherModel.sDownloadApps);
		}
	}

	public void setSystemAppFolder(boolean isSystemAppFolder){
		mIsSystemAppFolder = isSystemAppFolder;
	}
	
	// 按下时画背景色
	private void drawBubbleOutline(Canvas canvas)
	{
		int width = getWidth();
		int height = getHeight();
		
		canvas.save();
		canvas.translate(mScrollX, mScrollY);
		mOnTouchDrawble.setBounds(0, 0, width, height);
		mOnTouchDrawble.draw(canvas);
		canvas.restore();
	}

	// 申请用于桌面的视图
	public void applyAppFromBubbleView(SimpleItemInfo item, String strTitle)
	{
		setBackgroundResource(getResources().getIdentifier("bg_folder", "drawable", "com.tozed.android.launcher"));
		
		mAppTitle.setText(strTitle);
		setTag(item);
	}

	public String getText(){
		return mAppTitle.getText().toString();
	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		
		 if (!isPressed())
		      return;
		 
		 drawBubbleOutline(canvas);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		switch (event.getAction())
		{
		case MotionEvent.ACTION_MOVE:
		case MotionEvent.ACTION_DOWN:
			mPostDrawHandler.postDelayed(mDrawOutlineRunnable, 200);
			return super.onTouchEvent(event);
			
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			mPostDrawHandler.removeCallbacks(mDrawOutlineRunnable);
			
			if (isPressed())
				setPressed(false);
			postInvalidate();
			return super.onTouchEvent(event);
		}
		
		return super.onTouchEvent(event);
	}


}
